/*************************************************
Author: will
Date: 10/24/2012
Description:
Notes: 
*************************************************/

#ifndef __CMaterialConfig_h__
#define __CMaterialConfig_h__

#include "CTArray.h"
#include "commclass.h"
#include "commerrno.h"
#include "CDropItemSubType.h"

class CMaterialConfigData
{
public:
	CMaterialConfigData() 
        :_bHasConfig(false)
    {
        _iDropBeginTime = 0;
        _iDropEndTime = 0;
	}

	virtual ~CMaterialConfigData() 
    {
	}

public:
    bool _bHasConfig;

	unsigned short _usMaterialConfigID;
    unsigned short _usLevel;
    CTSizeString<DROP_ITEM_CFG_SUBTYPE_STR_LEN> _szDropItemSubType;
    CTSizeString<MAX_ICON_NAME_LEN> _szIconName;
    unsigned int   _uiSilverBuyPrice;
    unsigned int   _uiSilverSellPrice;
    unsigned int   _uiContriPrice;
    unsigned int   _uiGoldBuyPrice;    
    int   _iGoldSynPrice;       //合成时的金币买价
    CDropInstanceConfig _stDropInstace;

    int            _iDropBeginTime;  //掉落开始时间
    int            _iDropEndTime;    //掉落结束时间

    bool               _bCanAutoSellInIns;  //在副本中是否可以自动出售
};

class CMaterialConfig
{
public:
    CMaterialConfig() 
    {
    }

    virtual ~CMaterialConfig() 
    {
    }

    static size_t CountSize()
    {
        return CDropItemSubTypeConfig::CountSize(MAX_MATERIAL_CONFIG_COUNT);
    }
    int Init(const char* sBuff)
    {
        return _stDropItemSubTypeConfig.Init(sBuff, MAX_MATERIAL_CONFIG_COUNT);
    }
    int AddConfig(CMaterialConfigData& stConfigData)
    {
        int iRet = 0;
        if (0 == stConfigData._usMaterialConfigID 
            || stConfigData._usMaterialConfigID > MAX_MATERIAL_CONFIG_COUNT)
        {
            CT_ERROR(("error material config %d", stConfigData._usMaterialConfigID));
            return RET_SYSTEM_FAIL;
        }

        if (_astMaterialConfigDatas[stConfigData._usMaterialConfigID]._bHasConfig)
        {
            CT_ERROR(("duplicate material config %d", stConfigData._usMaterialConfigID));
            return RET_SYSTEM_FAIL;
        }

        _astMaterialConfigDatas[stConfigData._usMaterialConfigID] = stConfigData;
        _astMaterialConfigDatas[stConfigData._usMaterialConfigID]._bHasConfig = true;
        if (stConfigData._szDropItemSubType.GetDataLen() > 0)
        {
            CDropItemSubTypeConfigData stItemDropSubTypeData;
            stItemDropSubTypeData._iItemID = stConfigData._usMaterialConfigID;
            iRet = _stDropItemSubTypeConfig.AddSubType(stConfigData._szDropItemSubType, stItemDropSubTypeData);
            CHECK_RETURN(iRet);
        }
        return 0;
    }

    bool IsMaterialValid(unsigned short usMaterialConfigID)
    {
        if (0 == usMaterialConfigID 
            || usMaterialConfigID > MAX_MATERIAL_CONFIG_COUNT)
        {
            return false;
        }

        if (!_astMaterialConfigDatas[usMaterialConfigID]._bHasConfig)
        {
            return false;
        }

        return true;
    }

    int GetConfig(unsigned short usMaterialConfigID, CMaterialConfigData& stConfigData)
    {
        if (!IsMaterialValid(usMaterialConfigID))
        {
            return ERROR_ITEM_NO_MATERIAL_CONFIG;
        }

        stConfigData = _astMaterialConfigDatas[usMaterialConfigID];

        return 0;
    }

    int GetConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey, 
                    CMaterialConfigData& stConfigData)
    {
        int iRet = 0;
        CDropItemSubTypeConfigData stItemDropSubTypeData;
        iRet = _stDropItemSubTypeConfig.GetRandomData(szKey, stItemDropSubTypeData);
        CHECK_RETURN(iRet);
        unsigned short usMaterialConfigID = (unsigned short)stItemDropSubTypeData._iItemID;
        return GetConfig(usMaterialConfigID, stConfigData);
    }
public:
    CTLib::CTArray<CMaterialConfigData, MAX_MATERIAL_CONFIG_COUNT + 1> _astMaterialConfigDatas;
    CDropItemSubTypeConfig _stDropItemSubTypeConfig;
    CMaterialConfigData  _stSolventConfig;
};

class CSealConfigData
{
public:
    CSealConfigData()
        :_bHasConfig(false)
    {
        _usSealId = 0;
        _iBuySilverPrice = 0;
        _iBuyContPrice = 0;
        _iBuyGoldPrice = 0;
        _iSellSilverPrice = 0;
    }

public:
    bool _bHasConfig;
    unsigned short _usSealId; 
    int   _iBuySilverPrice;
    int   _iBuyGoldPrice;
    int   _iBuyContPrice;
    int   _iSellSilverPrice;
};

class CJewlConfigData
{
public:
    CJewlConfigData()
        :_bHasConfig(false)
    {
    }

    virtual ~CJewlConfigData()
    {
    }

public:
    bool _bHasConfig;
    unsigned short _usJewelId;    
    CTSizeString<DROP_ITEM_CFG_SUBTYPE_STR_LEN> _szDropItemSubType;
    unsigned int   _uiSilverBuyPrice;
    unsigned int   _uiSilverSellPrice;
};

class CJewlConfig
{
public:
    CJewlConfig()
    {
    }

    virtual ~CJewlConfig()
    {
    }

    int Init(const char* sBuff)
    {
        return _stDropItemSubTypeConfig.Init(sBuff, MAX_JEWEL_CONFIG_COUNT);
    }
    
    static size_t CountSize()
    {
        return CDropItemSubTypeConfig::CountSize(MAX_JEWEL_CONFIG_COUNT);
    }


    int AddConfig(CJewlConfigData& stConfigData)
    {
        int iRet = 0;

        if (0 == stConfigData._usJewelId
            || stConfigData._usJewelId > MAX_JEWEL_CONFIG_COUNT)
        {
            CT_ERROR(("error jewel config %d", stConfigData._usJewelId));
            return ERROR_JEWL_CONFIG_NONE;
        }

        if (_astJewelConfigDatas[stConfigData._usJewelId]._bHasConfig)
        {
            CT_ERROR(("duplicate scroll config %d", stConfigData._usJewelId));
            return ERROR_JEWL_CONFIG_NONE;
        }

        _astJewelConfigDatas[stConfigData._usJewelId] = stConfigData;
        _astJewelConfigDatas[stConfigData._usJewelId]._bHasConfig = true;

        //添加掉落类型配置
        if (stConfigData._szDropItemSubType.GetDataLen() > 0)
        {
            CDropItemSubTypeConfigData stItemDropSubTypeData;
            stItemDropSubTypeData._iItemID = stConfigData._usJewelId;
            iRet = _stDropItemSubTypeConfig.AddSubType(stConfigData._szDropItemSubType, stItemDropSubTypeData);
            CHECK_RETURN(iRet);
        }

        return 0;
    }

    int GetConfig(unsigned short usJewelId, CJewlConfigData& stConfigData)
    {
        if (0 == usJewelId
            || usJewelId > MAX_JEWEL_CONFIG_COUNT)
        {
            return ERROR_ITEM_NO_SCROLL_CONFIG;
        }

        if (!_astJewelConfigDatas[usJewelId]._bHasConfig)
        {
            return ERROR_ITEM_NO_SCROLL_CONFIG;
        }

        stConfigData = _astJewelConfigDatas[usJewelId];

        return 0;
    }

    int GetConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
        CJewlConfigData& stConfigData)
    {
        int iRet = 0;

        CDropItemSubTypeConfigData stItemDropSubTypeData;
        iRet = _stDropItemSubTypeConfig.GetRandomData(szKey, stItemDropSubTypeData);
        CHECK_RETURN(iRet);

        unsigned short usScrollConfigID = (unsigned short)stItemDropSubTypeData._iItemID;
        return GetConfig(usScrollConfigID, stConfigData);
    }

public:
    CTLib::CTArray<CJewlConfigData, MAX_JEWEL_CONFIG_COUNT + 1> _astJewelConfigDatas;
    CDropItemSubTypeConfig _stDropItemSubTypeConfig;    
};

class CDropPartnerConfigData
{
public:
    CDropPartnerConfigData()
        :_bHasConfig(false)
    {
    }

public:
    bool _bHasConfig;
    unsigned short _usPartnerID;    
    unsigned int   _uiSilverBuyPrice;
    unsigned int   _uiSilverSellPrice;
};

class CDropPartnerConfig
{
public:

    int AddConfig(CDropPartnerConfigData& stConfigData)
    {
        if (0 == stConfigData._usPartnerID
            || stConfigData._usPartnerID > MAX_PARTNER_CFG_COUNT)
        {
            CT_ERROR(("%d", stConfigData._usPartnerID));
            return RET_SYS_ERROR;
        }

        if (_astDropPartnerConfigDatas[stConfigData._usPartnerID]._bHasConfig)
        {
            CT_ERROR(("%d", stConfigData._usPartnerID));
            return RET_SYS_ERROR;
        }

        _astDropPartnerConfigDatas[stConfigData._usPartnerID] = stConfigData;
        _astDropPartnerConfigDatas[stConfigData._usPartnerID]._bHasConfig = true;

        return 0;
    }

    int GetConfig(unsigned short usPartnerID, CDropPartnerConfigData& stConfigData)
    {
        if (0 == usPartnerID
            || usPartnerID > MAX_PARTNER_CFG_COUNT)
        {
            return RET_SYS_ERROR;
        }

        if (!_astDropPartnerConfigDatas[usPartnerID]._bHasConfig)
        {
            return RET_SYS_ERROR;
        }

        stConfigData = _astDropPartnerConfigDatas[usPartnerID];

        return 0;
    }

public:
    CTLib::CTArray<CDropPartnerConfigData, MAX_PARTNER_CFG_COUNT + 1> _astDropPartnerConfigDatas; 
};

#endif // __CMaterialConfig_h__

